package com.aaa.member.service.impl;

import com.aaa.entity.Member;
import com.aaa.member.dao.MemberDao;
import com.aaa.member.service.MemberService;
import com.aaa.security.util.WebUtils;
import com.aaa.util.JwtUtil;
import com.aaa.vo.HeadVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Author 崔灿
 * @Date 2023/03/04 08:58
 * @ClassName MemberServiceImpl
 * @Description
 * @Version 1.0
 */

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public Result selectMember(Integer currentPage , Integer pageSize , Member member) {
        PageHelper.startPage(currentPage, pageSize);
        List<Member> members = memberDao.selectmember(member);
        PageInfo<Member> memberss = new PageInfo<>(members);
        return new Result(2000,"查询成功",memberss);
    }

    @Override
    public Result update(Member member) {
        int i = memberDao.updateById(member);
        return new Result(2000,"修改状态成功",i);
    }

    @Override
    public Result getMember() {
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        String username = (String) tokenChaim.get("username");
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        Member member = memberDao.selectOne(wrapper);
        return new Result(2000,"成功",member);
    }

    @Override
    public Result uploadImg(HeadVo headVo) {
        Integer integer = memberDao.uploadImg(headVo);
        return new Result(2000,"成功",integer);
    }

    @Override
    public Integer updateAmount(Integer id, Double accountBalance) {
        Member member = memberDao.selectById(id);
        Double accountBalance1 = member.getAccountBalance();
        double accountBalance2 = accountBalance1 - accountBalance;
        return memberDao.updateAmount(id,accountBalance2);
    }
}
